package Articulo;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author nan
 */
public class MedGIngreso implements ActionListener, KeyListener{

    private GIngreso Ingreso = new GIngreso();
    private Conexion.ConexionBD conectar = new Conexion.ConexionBD();
    private Articulo.articulosListar articulos;

    public MedGIngreso(){
        Ingreso.setActionListeners(this);
        Ingreso.setKeyListener(this);
        Ingreso.setLocationRelativeTo(null);
        Ingreso.show();
    }

    public MedGIngreso(Articulo.articulosListar articulos){
        Ingreso.setActionListeners(this);
        Ingreso.setKeyListener(this);
        Ingreso.setLocationRelativeTo(null);
        Ingreso.show();
        this.articulos = articulos;
    }

    public void setVisible(boolean mostrar){
        Ingreso.setVisible(mostrar);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == Ingreso.getjButtonAceptar()){
            String a = (Ingreso.getjTextFieldcodArt().getText());
            String aa = (Ingreso.getjTextFieldNombre().getText());
            String b =(Ingreso.getjTextFieldmarca().getText());
            String c =(Ingreso.getjTextFielddescripcion().getText());
            float d = Float.parseFloat(Ingreso.getjTextFieldprecioCosto().getText());
            String ee =(Ingreso.getjTextFieldStockMin().getText());
            String f =(Ingreso.getjTextFieldStockMax().getText());
            String g =(Ingreso.getStock().getText());
            conectar.conectar();
            Statement st;
            if(Integer.parseInt(ee)>Integer.parseInt(f)){
                JOptionPane.showMessageDialog(null, "El valor de Stock minimo es mas Grande que el Valor de Stock Maximo", "Error en Stock", JOptionPane.WARNING_MESSAGE);
            }else{
                try {
                    st = conectar.getConn().createStatement();
                    String insercion = "INSERT INTO \"Articulo\"(\"codArt\", nombre,marca, descripcion, \"precioCosto\", \"StockMin\", \"StockMax\", \"Stock\") VALUES ('"+a+"','"+aa+"','"+b+"','"+c+"',"+d+","+ee+","+f+","+g+")";
                    st.execute(insercion);
                    this.articulos.actualizarTabla();
                    JOptionPane.showMessageDialog(null, "Se ha insertado el Articulo", "Elemento insertado", JOptionPane.INFORMATION_MESSAGE);
                } catch (SQLException ex) {
                    JOptionPane.showMessageDialog(null, "Nose puede insertar", "ERROR", JOptionPane.ERROR_MESSAGE);
                }
                conectar.cerrar();
                Ingreso.dispose();
                this.vaciarCampos();
            }
        }else{
            if(e.getSource() == Ingreso.getjButtonCancelar()){
                Ingreso.dispose();
                this.vaciarCampos();
            }
        }
    }

    public void keyTyped(KeyEvent e) {
        if (e.getSource()== Ingreso.getjTextFieldcodArt()){
            char caracter = e.getKeyChar();
            if((caracter == '$')){
                e.consume();
            }
        }
        if (e.getSource()== Ingreso.getjTextFieldStockMin()){
            char caracter = e.getKeyChar();
            if(((caracter < '0') || (caracter > '9')) && (caracter != KeyEvent.VK_BACK_SPACE) ){
                e.consume();
            }
        }
        if (e.getSource()== Ingreso.getjTextFieldStockMax()){
            char caracter = e.getKeyChar();
            if(((caracter < '0') || (caracter > '9')) && (caracter != KeyEvent.VK_BACK_SPACE) ){
                e.consume();
            }
        }
        if (e.getSource()== Ingreso.getjTextFieldprecioCosto()){
            char caracter = e.getKeyChar();
            if(((caracter < '0') || (caracter > '9')) && (caracter != KeyEvent.VK_BACK_SPACE) && (caracter != '.')){
                e.consume();
            }
            if (caracter == '.' && Ingreso.getjTextFieldprecioCosto().getText().contains(".")){
                e.consume();
            }
        }
        if (e.getSource()== Ingreso.getStock()){
            char caracter = e.getKeyChar();
            if(((caracter < '0') || (caracter > '9')) && (caracter != KeyEvent.VK_BACK_SPACE) ){
                e.consume();
            }
        }
    }

    public void keyPressed(KeyEvent e) {

        if (e.getSource() == Ingreso.getjTextFieldcodArt() && e.getKeyChar() == KeyEvent.VK_ENTER){
            Ingreso.getjTextFieldNombre().requestFocus();
        } if (e.getSource() == Ingreso.getjTextFieldNombre() && e.getKeyChar() == KeyEvent.VK_ENTER){
            Ingreso.getjTextFieldmarca().requestFocus();
        }if (e.getSource() == Ingreso.getjTextFieldmarca() && e.getKeyChar() == KeyEvent.VK_ENTER){
            Ingreso.getjTextFielddescripcion().requestFocus();
        }if (e.getSource() == Ingreso.getjTextFielddescripcion() && e.getKeyChar() == KeyEvent.VK_ENTER){
            Ingreso.getjTextFieldprecioCosto().requestFocus();
        }if (e.getSource() == Ingreso.getjTextFieldprecioCosto() && e.getKeyChar() == KeyEvent.VK_ENTER){
            Ingreso.getjTextFieldStockMin().requestFocus();
        } if (e.getSource() == Ingreso.getjTextFieldStockMin() && e.getKeyChar() == KeyEvent.VK_ENTER){
            Ingreso.getjTextFieldStockMax().requestFocus();
        } if (e.getSource() == Ingreso.getjTextFieldStockMax() && e.getKeyChar() == KeyEvent.VK_ENTER){
            Ingreso.getStock().requestFocus();
        }if (e.getSource() == Ingreso.getStock() && e.getKeyChar() == KeyEvent.VK_ENTER){
            Ingreso.getjButtonAceptar().doClick();
        }

    }

    public void keyReleased(KeyEvent e) {   }

    public void vaciarCampos(){
        Ingreso.getjTextFieldcodArt().setText("");
        Ingreso.getjTextFieldNombre().setText("");
        Ingreso.getjTextFieldmarca().setText("");
        Ingreso.getjTextFielddescripcion().setText("");
        Ingreso.getjTextFieldprecioCosto().setText("");
        Ingreso.getjTextFieldStockMin().setText("");
        Ingreso.getjTextFieldStockMax().setText("");
        Ingreso.getStock().setText("");
    }
    
}
